#!/bin/zsh

# {{{ LICENSE

# Copyright (C) 2012-2013 Denis Roio <jaromil@dyne.org>
#
# This source  code is free  software; you can redistribute  it and/or
# modify it under the terms of  the GNU Public License as published by
# the Free  Software Foundation; either  version 3 of the  License, or
# (at your option) any later version.
#
# This source code is distributed in  the hope that it will be useful,
# but  WITHOUT ANY  WARRANTY;  without even  the  implied warranty  of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# }}}

# {{{ GLOBAL VARIABLES

# ZShaolin system build and basic configuration

FORCE=${FORCE:-0}
QUIET=${QUIET:-0}
DEBUG=${DEBUG:-0}

#TARGET=arm-linux-androideabi
# same value should be the bootstrap target

# the location inside android
APKPATH=/data/data/org.dyne.zshaolin



LOGS=build.log

## Architecture compiler flags
#ARCH="-mfloat-abi=softfp -march=armv7-a -mtune=cortex-a8"
# very old devices: ARCH="-march=armv5 -mfloat-abi=softfp"

REPO="http://releases.dyne.org/zshaolin/sources"


autoload colors; colors
# standard output message routines
# it's always useful to wrap them, in case we change behaviour later
notice() { if [[ $QUIET == 0 ]]; then print "$fg_bold[green][*]$fg_no_bold[default] $1" >&2; fi }
error()  { if [[ $QUIET == 0 ]]; then print "$fg[red][!]$fg[default] $1" >&2; fi }
func()   { if [[ $DEBUG == 1 ]]; then print "$fg[blue][D]$fg[default] $1" >&2; fi }
act()    {
    if [[ $QUIET == 0 ]]; then
	if [ "$1" = "-n" ]; then
	    print -n "$fg_bold[white] . $fg_no_bold[default] $2" >&2;
	else
	    print "$fg_bold[white] . $fg_no_bold[default] $1" >&2;
	fi
    fi
}

typeset ZHOME

{ test "$ZHOME" = "" } && {
    error "Set ZHOME environment to Zshaolin path"
    # where are we (configure accordingly)
#    ZHOME=${ZHOME:-/srv/zshaolin}
#   act "using default: $ZHOME"
    return 1
}


enter() {
    if ! [ -r $1/conf.zsh ]; then
	error "build directory misses configuration file: $1/conf.zsh"
	return 1
    fi
    pushd $1
    act "Enter `pwd`"
    shift
    source conf.zsh
#    popd
}

list_sources() {
	touch /tmp/zshaolin.list
	typeset -a modules
	if [ "${#@}" = "1" ]; then modules=(`find ${ZHOME}/build -type f -name Sources`)
	else
	    shift
	    for m in ${=@}; do
		modules+=(${ZHOME}/build/${m}/Sources); done
	fi
	for i in  $modules; do
		cat $i | awk '!/^#/ { print $1 $2 " " $4 }' >> /tmp/zshaolin.list
	done
	swlist=`cat /tmp/zshaolin.list | sort | uniq | sed 's/_arm//;s/eabi//'`
	rm -f /tmp/zshaolin.list
	for i in ${(f)swlist}; do
		name=${i[(ws: :)1]}
		url=${i[(ws: :)2]}
		if [ "$url" = "$name" ]; then
			print "<li>$name</li>"
		else
			print "<li><a href=\"${url}\" target=\"_blank\">$name</a></li>"
		fi
	done
}

typeset -h operation
typeset -h module



case "$1" in

# here module is set with the second argument, after the commands listed here:
    build)
	operation=build
	module=${2:-all}
	enter build/ ${=@}
       	;;

# same here. if no setting specified, it becomes "all".
# internal conf.zsh supports also "list" to list options
    pack)
	operation=pack
	module=${2:-list}
	enter pack/ ${=@}
	;;

    clean)
	operation=clean
	module=${2:-all}
	{ test $module = all } && { rm -rf $PREFIX }
	enter build/ ${=@}
	;;

    # this can be called from commandline
    compile)
	args=($@) && shift args
	operation=compile
	zconfigure ${args}
	;;

    # this initialized the current shell with the zshaolin setup
    test)
	ZHOME=$ZHOME zsh system/etc/zlogin
	;;

    source)
	return ;;
    list)
	list_sources ${=@}
	;;
    help|-h)
	error "No help. We're all on our own, for now."
	;;
    "")
	error "No command specified, see help" ;;
    *)
	error "Command not found: $@" ;;

esac
